// ================================================================================
// 		File: PartyGrassrootsWorkTrend.cs
// 		Desc: 
//  
// 		Called by:   
//               
// 		Auth: chenwenzhi（）
// 		Date: 2016-06-11 23:56
// ================================================================================
// 		Change History
// ================================================================================
// 		Date:		Author:				Description:
// 		--------	--------			-------------------
//    
// ================================================================================
// 
// ================================================================================
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;
using ZHDJ.Core;

namespace ZHDJ.Data.Models.Grassroots
{
    /// <summary>
    /// 工作纪实
    /// </summary>   
    public class PartyGrassrootsWorkTrend : EntityBase
    {
        #region 属性


        /// <summary>
        /// 分组
        /// </summary>
        public string ItemID { get; set; }

        /// <summary>
        /// 标题
        /// </summary>
        public string Title { get; set; }

        /// <summary>
        /// 工作时间
        /// </summary>
        public DateTime? WorkTime { get; set; }

        /// <summary>
        /// 内容
        /// </summary>
        public string Content { get; set; }

        /// <summary>
        /// 状态1草稿 2发布
        /// </summary>
        public int Status { get; set; }

        /// <summary>
        /// 0、无；1、置顶；2、固顶；3、置顶+固顶；
        /// </summary>
        public int Flags { get; set; }

        /// <summary>
        /// 创建用户
        /// </summary>
        public string CreateBy { get; set; }

        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime? CreateTime { get; set; }

        /// <summary>
        /// 更新时间
        /// </summary>
        public DateTime? ModifyTime { get; set; }

        /// <summary>
        /// 修改用户
        /// </summary>
        public string ModifyBy { get; set; }

        #endregion

        #region 查询管理
        public static DataSet GetWorkTrendData(int pageIndex, int pageSize, string groupId, string itemId, string searchKey, string status)
        {
            List<MySqlParameter> parameters = new List<MySqlParameter>()
            {
                new MySqlParameter()
                {
                    DbType = DbType.Int32,
                    ParameterName = "Begin",
                    Value = (pageIndex)*pageSize
                },
                new MySqlParameter()
                {
                    DbType = DbType.Int32,
                    ParameterName = "PageSize",
                    Value = pageSize
                }
            };

            StringBuilder sb = new StringBuilder();
            sb.Append(@"select SQL_CALC_FOUND_ROWS trend.ID,Title,if(Status=1,'否','是') as StatusName,
case Flags when 0 then '无' when 1 then '置顶' when 2 then '固顶' when 3 then '置顶+固顶' end as FlagsName,
trend.CreateTime,WorkTime,ItemName ,trend.WorkTime
from tblpartygrassrootsworktrend trend
left join tblpartygrassrootsgroupitem item on trend.ItemID=item.ID
left join tblpartygrassrootsgroup g ON item.GroupID=g.ID where 1=1");

            if (!string.IsNullOrEmpty(groupId))
            {
                sb.Append(" and g.ID=?GroupID");
                parameters.Add(new MySqlParameter() { DbType = DbType.String, ParameterName = "GroupID", Value = groupId });
            }
            if (!string.IsNullOrEmpty(itemId))
            {
                sb.Append(" and trend.ItemID=?ItemID");
                parameters.Add(new MySqlParameter() { DbType = DbType.String, ParameterName = "ItemID", Value = itemId });
            }
            if (!string.IsNullOrEmpty(searchKey))
            {
                sb.Append(" and Title like ?Title ");
                parameters.Add(
                    new MySqlParameter()
                    {
                        DbType = DbType.String,
                        ParameterName = "Title",
                        Value = string.Format("%{0}%", searchKey)
                    });
            }
            if (!string.IsNullOrEmpty(status))
            {
                sb.Append(" and Status=?Status");
                parameters.Add(
                    new MySqlParameter()
                    {
                        DbType = DbType.String,
                        ParameterName = "Status",
                        Value = status
                    });
            }

            sb.Append(" order by Status desc, Flags desc, CreateTime desc LIMIT ?Begin ,?PageSize;SELECT FOUND_ROWS();");
            return MySqlHelper.ExecuteDataSet(sb.ToString(), parameters.ToArray());
        }
        #endregion
    }
}
